package com.jbit.examSystem.service.impl;

import com.jbit.examSystem.dao.ErrorQuestionDao;
import com.jbit.examSystem.dao.ExamDetailDao;
import com.jbit.examSystem.dao.ScoreDao;
import com.jbit.examSystem.param.RankingParam;
import com.jbit.examSystem.pojo.ErrorQuestion;
import com.jbit.examSystem.pojo.ExamDetail;

import com.jbit.examSystem.pojo.Score;
import com.jbit.examSystem.service.ScoreService;
import com.jbit.examSystem.pojo.ScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ErrorQuestionDao errorQuestionDao;
    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private ExamDetailDao examDetailDao;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<ScoreVo> list(Integer stuNo) {
        return scoreDao.list(stuNo);
    }

    @Override
    public List<Score> getAllScores(RankingParam param) {
        return scoreDao.getAllScores(param);
    }

    @Override
    public Score getScoreByExamIdAndStudentId(Integer studentId, Integer examId) {
        Score score = scoreDao.getScoreByExamIdAndStudentId(studentId, examId);
        return score;
    }

    @Override
    public void saveExamInfo(Score score, ExamDetail[] examDetails) {
        //保存成绩信息
        System.out.println(score);
        Integer scoreId = scoreDao.addScore(score);
        //保存考试明细
        saveExamDetail(examDetails, score.getId());
    }

    //保存考试明细
    private void saveExamDetail(ExamDetail[] examDetails, Integer scoreId) {
        for (ExamDetail examDetail : examDetails) {
            examDetail.setScoreId(scoreId);
            examDetailDao.addExamDetail(examDetail);
        }
    }

}
